#include "ShapeResolver.h"

bool ShapeResolver::mInit = false;
ShapeResolver::StringShapeMap ShapeResolver::shapeMap;

ShapeResolver::Shape ShapeResolver::getShape(const std::string& s)
{
	if(!mInit) init();

//        std::transform(
//            s.begin(),
//            s.end(),
//            s.begin(),
//            (int(*)(int)) tolower);
			
	StringShapeMap::iterator it = shapeMap.find(s);
	if(it == shapeMap.end())
		throw std::runtime_error("Unable to get shape " + s);

	return shapeMap[s];
}

void ShapeResolver::init()
{
	shapeMap["box"] = BOX;
	shapeMap["sphere"] = SPHERE;
	shapeMap["cylinder"] = CYLINDER;
	shapeMap["trimesh"] = TRIMESH;
	shapeMap["convex"] = CONVEX;
	ShapeResolver::mInit=true;
}


btCollisionShape* ShapeResolver::getShape(const std::string& s, Ogre::Entity* e)
{
	if(!mInit) init();

	Ogre::Matrix4 mat = Ogre::Matrix4::IDENTITY;
	Ogre::SceneNode* node = e->getParentSceneNode();
	mat.setScale(node->getScale());
		
	BtOgre::StaticMeshToShapeConverter converter(e, mat);
	btCollisionShape *shape;

	switch (getShape(s))
		{
		case BOX: shape = converter.createBox(); break;
		case SPHERE: shape = converter.createSphere(); break;
		case CYLINDER: shape = converter.createCylinder();	break;
		case TRIMESH: shape = converter.createTrimesh(); break;
		case CONVEX:
		default: shape = converter.createConvex();	break;
		}
	return shape;
}
